# coding:utf8
import sys
import pymysql as MySQLdb
import datetime
import pandas as pd
import numpy as np
import os
import stat


#导出rating到cvs

host = '127.0.0.1'
user = 'root'
pwd = '123456'
db = 'll_newscms'
sql = 'SELECT ll_uid,ll_aid,ll_rating,ll_time from ll_rating ORDER BY ll_rid DESC '
# sheet_name = 'building'



conn = MySQLdb.connect(host,user,pwd,db,charset='utf8')
cursor = conn.cursor()
count = cursor.execute(sql)
print(count)

cursor.scroll(0,mode='absolute')
results = cursor.fetchall()
fields = cursor.description

datapath = "/home/lili/project/recommend/ll_newscms/public/data"
file="/home/lili/project/recommend/ll_newscms/public/data/rating.txt"


if not os.path.exists(datapath) :
    os.makedirs(datapath,0777)

if not os.path.exists(file):
    os.mknod(file,0777)


os.system('chmod 777 -R '+datapath)


np_data = np.array(results)
# print results
# print(np_data)
save = pd.DataFrame(np_data, columns = ['userId','AID','rating','timestamp'])
save.to_csv(file,index=False)  #index=False,header=False表示不保存行索引和列标题







# workbook = xlwt.Workbook()
# sheet = workbook.add_sheet(sheet_name,cell_overwrite_ok=True)
#
# for field in range(0,len(fields)):
#     sheet.write(0,field,fields[field][0])
#
# row = 1
# col = 0
# for row in range(1,len(results)+1):
#     for col in range(0,len(fields)):
#         sheet.write(row,col,u'%s'%results[row-1][col])
#
# workbook.save(out_path)
